package com.xqboss.apps.service.punch;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xqboss.apps.domain.punch.PunchUserRecord;
import com.xqboss.apps.dto.punch.GetWebPunchRecordDto;
import com.xqboss.apps.mapper.punch.PunchUserRecordMapper;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * <p>
 *     用户打卡记录服务
 * </p>
 * @author Trent
 * @date 2023/8/15
 */
@Service
public class PunchUserRecordService extends ServiceImpl<PunchUserRecordMapper, PunchUserRecord> {

    /**
     * 获取用户的打卡记录
     * @param userId
     * @return
     */
    public List<PunchUserRecord> getUserPunchRecordList(Long userId){
        List<PunchUserRecord> list = list(new LambdaQueryWrapper<PunchUserRecord>()
                .eq(PunchUserRecord::getUserId, userId));
        return list;
    }

    /**
     * 获取打卡记录
     * @param params 筛选参数
     * @return
     */
    public List<PunchUserRecord> getUserPunchRecordList(GetWebPunchRecordDto params){
        LambdaQueryWrapper<PunchUserRecord> queryWrapper = new LambdaQueryWrapper<>();
        if(Objects.nonNull(params.getUserId())){
            queryWrapper.eq(PunchUserRecord::getUserId, params.getUserId());
        }
        if(Objects.nonNull(params.getType())){
            queryWrapper.eq(PunchUserRecord::getType, params.getType());
        }
        if(Objects.nonNull(params.getCreateTimeStart())){
            queryWrapper.ge(PunchUserRecord::getCreateTime, params.getCreateTimeStart());
        }
        if(Objects.nonNull(params.getCreateTimeEnd())){
            queryWrapper.le(PunchUserRecord::getCreateTime, params.getCreateTimeEnd());
        }
        queryWrapper.orderByDesc(PunchUserRecord::getCreateTime);
        return list(queryWrapper);
    }

    /**
     * 获取用户最后一次打卡
     */
    public PunchUserRecord getLastRecord(Long userId) {
        PunchUserRecord record = getOne(new LambdaQueryWrapper<PunchUserRecord>()
                .eq(PunchUserRecord::getUserId, userId)
                .orderByDesc(PunchUserRecord::getCreateTime)
                .last("LIMIT 1"));
        return record;
    }

    /**
     * 统计今日团队打卡次数
     * @param teamId
     * @return
     */
    public long countTeamTodayPunch(Long teamId, LocalDate localDate){
        return count(new LambdaQueryWrapper<PunchUserRecord>()
                .eq(PunchUserRecord::getTeamId, teamId)
                .between(PunchUserRecord::getCreateTime, LocalDateTime.of(localDate, LocalTime.MIN), LocalDateTime.of(localDate, LocalTime.MAX)));
    }

    /**
     * 统计今日团队打卡次数
     *
     * v1.6.0 只获取当日在队伍中的用户的所有打卡数
     *
     * @param teamId
     * @return
     */
    public long countTeamTodayPunchNew(Long teamId, LocalDate localDate){
        return getBaseMapper().countTeamTodayPunch(teamId,localDate);
    }

    /**
     * 统计指定用户今日打卡次数
     *
     * @param teamId 队伍id
     * @param userId 用户id
     * @return
     */
    public long countTodayPunchUser(Integer teamId, Long userId, LocalDate localDate) {
        return lambdaQuery()
                .eq(PunchUserRecord::getTeamId, teamId)
                .eq(PunchUserRecord::getUserId,userId)
                .between(PunchUserRecord::getCreateTime, LocalDateTime.of(localDate, LocalTime.MIN), LocalDateTime.of(localDate, LocalTime.MAX))
                .count();
    }

    /**
     * 统计队伍总打卡次数
     * @param teamId
     * @return
     */
    public Long countTeamTotalPunch(Long teamId){
        return count(new LambdaQueryWrapper<PunchUserRecord>()
                .eq(PunchUserRecord::getTeamId, teamId));
    }



    /**
     * 统计今日团队打卡用户总数
     *
     * @param teamId
     * @return
     */
    public int countTeamTodayPunchUser(Long teamId, LocalDate date){
        return baseMapper.countTeamPunchUser(teamId, date);
    }


    /**
     * 获取队伍指定日期的打卡用户列表
     * @param teamId
     * @param date
     */
    public List<Long> getTeamPunchUserList(Integer teamId, LocalDate date) {
        List<PunchUserRecord> list = list(new LambdaQueryWrapper<PunchUserRecord>()
                .select(PunchUserRecord::getUserId)
                .eq(PunchUserRecord::getTeamId, teamId)
                .between(PunchUserRecord::getCreateTime, LocalDateTime.of(date, LocalTime.MIN), LocalDateTime.of(date, LocalTime.MAX))
                .groupBy(PunchUserRecord::getUserId)
        );
        return list.stream().map(PunchUserRecord::getUserId).collect(Collectors.toList());
    }


    /***
     * 获取用户指定日期队伍打卡总收益
     * @param userId
     * @param teamId
     * @param date
     */
    public BigDecimal getUserTotalIncome(Long userId, Long teamId, LocalDate date) {
        return baseMapper.getUserTotalIncome(userId, teamId, date);
    }

    /**
     * 统计指定日期用户打卡规则的打卡次数
     * @param userId
     * @param ruleId
     * @param date
     * @return
     */
    public long countUserRulePunch(Long userId, Long ruleId, LocalDate date) {
        return count(new LambdaQueryWrapper<PunchUserRecord>()
                .eq(PunchUserRecord::getUserId, userId)
                .eq(PunchUserRecord::getPunchRuleId, ruleId)
                .between(PunchUserRecord::getCreateTime, LocalDateTime.of(date, LocalTime.MIN), LocalDateTime.of(date, LocalTime.MAX)));
    }

    /**
     * 统计指定日期用户(使用装备)打卡规则的打卡次数
     * @param userId
     * @param ruleId
     * @param date
     * @return
     */
    public long countUserRuleNftNumberIdPunch(Long userId, Long ruleId, Long nftNumberId, LocalDate date) {
        return count(new LambdaQueryWrapper<PunchUserRecord>()
                .eq(PunchUserRecord::getUserId, userId)
                .eq(PunchUserRecord::getNftNumberId, nftNumberId)
                .eq(PunchUserRecord::getPunchRuleId, ruleId)
                .between(PunchUserRecord::getCreateTime, LocalDateTime.of(date, LocalTime.MIN), LocalDateTime.of(date, LocalTime.MAX)));
    }



}
